package com.itheima.test4;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) throws IOException {
//        需求：使用TCP协议实现客户端和服务端互相发送消息。
//客户端发送一条消息，服务器读取之后，返回一条消息给客户端，
// 客户端读取服务器返回的消息之后，又发送一条消息…循环执行，直到输入exit。

        Lo:
        while (true) {
            Socket socket = new Socket("127.0.0.1", 33333);
            OutputStream os = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(osw);
            Scanner sc = new Scanner(System.in);
            String data1 = sc.next();
            osw.write(data1);
            osw.flush();
            // osw.close();
            socket.shutdownOutput();

            InputStream is = socket.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String data;
            while ((data = br.readLine()) != null) {
                if ("exit".equalsIgnoreCase(data)) {
//                    socket.close();
                    bw.write("exit");
                    bw.flush();
                    System.exit(0);
                    //break Lo;
                }
                System.out.println(data);
            }
            socket.close();
        }


    }
}
